package business;

import java.util.List;

import persistence.Periodical;
import persistence.PeriodicalDAO;
import persistence.PredefinedEntry;
import persistence.exception.DAOException;
import business.exception.BusinessException;

public class PeriodicalLogicImpl implements PeriodicalLogic {
	public static final String KIND_OF_PUBLICATION_PARAM = "Erscheinungsweise";
	private CommonLogic commonLogic;
	private PeriodicalDAO periodicalDAO;
	
	public void setCommonLogic(CommonLogic commonLogic) {
		this.commonLogic = commonLogic;
	}	
	public PeriodicalDAO getPeriodicalDAO() {
		return periodicalDAO;
	}
	public void setPeriodicalDAO(PeriodicalDAO periodicalDAO) {
		this.periodicalDAO = periodicalDAO;
	}	

	@Override
	public List<Periodical> getAllPeriodicals() throws BusinessException {
		try {
			return periodicalDAO.findAll();
		}
		catch (DAOException de) {
			throw new BusinessException("Problem when attempting to get all periodicals", de);
		}
	}
	
	@Override
	public List<PredefinedEntry> getKindsOfPublication() throws BusinessException{
		List<PredefinedEntry> kindsOfPublication = null;
		kindsOfPublication = commonLogic.findEntriesForParam(KIND_OF_PUBLICATION_PARAM);
		return kindsOfPublication;
	}
	@Override
	public Periodical findGivenPeriodical(int id) throws BusinessException {
		Periodical periodical = null;
		try {
			periodical = periodicalDAO.find(id);
		}
		catch (DAOException de) {
			throw new BusinessException("Problem when attempting to find Periodical with id: " + id, de);
		}
		return periodical;
	}

}
